﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace TEAA
{
    public class FileManager
    {

        /// <summary>
        /// Create a new folder on the file system
        /// </summary>
        /// <param name="path">The folder will be created to this path</param>
        /// <param name="folderName">Name of the folder to be created</param>
        /// <returns>Path to the created folder</returns>
        /// <exception cref="Exception">Couldn't create new folder</exception>
        public static string CreateNewFolder(string path, string folderName)
        {
            string folderPath = path + Path.DirectorySeparatorChar + folderName;

            if (Directory.Exists(folderPath))
                throw new Exception("Directory " + folderPath + " already exists!");
            Directory.CreateDirectory(folderPath);

            return Path.GetFullPath(folderPath);
        }

        /// <summary>
        /// Create a new file on the file system
        /// </summary>
        /// <param name="path">The file will be created to this path</param>
        /// <param name="fileName">Name of the file to be created</param>
        /// <returns>Path to the created file</returns>
        /// <exception cref="Exception">Couldn't create new file</exception>
        public static string CreateNewFile(string path, string fileName)
        {
            string filePath = path + Path.DirectorySeparatorChar + fileName;

            if (File.Exists(filePath))
                throw new Exception("File " + filePath + " already exists!");
            File.Create(filePath).Close();

            return Path.GetFullPath(filePath);
        }

        /// <summary>
        /// Delete a folder on the file system
        /// </summary>
        /// <param name="folderPath">Path to the folder</param>
        /// <returns>Path to deleted folder's parent</returns>
        /// <exception cref="Exception">Couldn't delete folder</exception>
        public static string DeleteFolder(string folderPath)
        {
            string path = Path.GetDirectoryName(folderPath);

            Directory.Delete(folderPath, true);

            return Path.GetFullPath(path);
        }

        /// <summary>
        /// Delete a file on the file system
        /// </summary>
        /// <param name="filePath">Path to the file</param>
        /// <returns>Path to deleted file's folder</returns>
        /// <exception cref="Exception">Couldn't delete file</exception>
        public static string DeleteFile(string filePath)
        {
            string path = Path.GetDirectoryName(filePath);

            if (!File.Exists(filePath))
                throw new Exception("File " + filePath + " doesn't exist!");
            File.Delete(filePath);

            return Path.GetFullPath(path);
        }

        /// <summary>
        /// Rename folder on the file system
        /// </summary>
        /// <param name="folderPath">Path to the folder</param>
        /// <param name="newFolderName">New name to rename the folder to</param>
        /// <returns>Renamed folder's path</returns>
        /// <exception cref="Exception">Couldn't rename folder</exception>
        public static string RenameFolder(string folderPath, string newFolderName)
        {
            string path = Path.GetDirectoryName(folderPath);
            string newFolderPath = path + Path.DirectorySeparatorChar + newFolderName;

            Directory.Move(folderPath, newFolderPath);

            return Path.GetFullPath(newFolderPath);
        }

        /// <summary>
        /// Rename file on the file system
        /// </summary>
        /// <param name="filePath">Path to the file</param>
        /// <param name="newFileName">New name to rename the file to</param>
        /// <returns>Renamed file's path</returns>
        /// <exception cref="Exception">Couldn't rename file</exception>
        public static string RenameFile(string filePath, string newFileName)
        {
            string path = Path.GetDirectoryName(filePath);
            string newFilePath = path + Path.DirectorySeparatorChar + newFileName;

            File.Move(filePath, newFilePath);

            return Path.GetFullPath(newFilePath);
        }

        /// <summary>
        /// Get containing folders of a given path
        /// </summary>
        /// <param name="folderPath">Path to folder to get get containing folders</param>
        /// <returns>Folders found in path folder</returns>
        public static List<string> GetFolders(string folderPath)
        {
            List<string> foldersList = new List<string>();

            if (Directory.Exists(folderPath))
            {
                string[] folders = Directory.GetDirectories(folderPath);
                foreach (string folder in folders)
                {
                    string folderName = Path.GetFileName(folder);
                    foldersList.Add(folderName);
                }
            }

            return foldersList;
        }

    }
}
